package com.lyj.job;

import com.lyj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
public class TimedJob {
    @Autowired
    private UserService userService;

//    @Scheduled(cron = "0/5 * * * * ?")
    public void jobTest(){
        SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
        String format = sdf.format(new Date());
        System.out.println("现在时间：" + format);
    }

    @Scheduled(cron = "0 0/1 * * * ? ")    //从0分钟开始每一分钟执行一次
    public void deleteAvatar(){
        System.out.println("进行删除空闲状态的头像");
        List<String> avatars = userService.findAllAvatar();
//        System.out.println("被使用的头像数：" + avatars.size());

        String path = "D:\\Soft\\apache-tomcat-9.0.55\\webapps\\HRM\\images\\avatars";
        File file = new File(path);
        File[] files = file.listFiles();
//        System.out.println("头像库中的头像数量：" + files.length);

        for (File file1 : files) {
            String fileName = file1.getName();
            if (!avatars.contains(fileName) && !fileName.equals("default.png")){
                file1.delete();
            }
        }
    }
}
